package com.woniuxy.yoga.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.pojo.UserVo;
import com.woniuxy.yoga.pojo.Users;
import com.woniuxy.yoga.service.OrdersService;
import com.woniuxy.yoga.service.UsersService;
import com.woniuxy.yoga.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.yoga.pojo.Course;
import com.woniuxy.yoga.pojo.Orders;
import com.woniuxy.yoga.service.CourseService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author axq
 * @since 2020-10-28
 */
@RestController
@RequestMapping("/yoga/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "查看俱乐部下学员")
    @GetMapping(value = "/club2user")
    @CrossOrigin
    public R getCoachByPage(@ApiParam(value = "当前页")@RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
                            @ApiParam(value = "每页的记录数")@RequestParam(value = "limit",required = true,defaultValue = "6") Integer limit,
                            UserVo userVo) {
        //创建Page对象
        Page pageBean = new Page<>(page, limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(userVo.getNickname())) {
            queryWrapper.like("nickname", userVo.getNickname());
        }
        if (userVo.getPayee() != null) {
            queryWrapper.eq("payee", userVo.getPayee());
        }
        System.out.println(userVo.getPayee());
        Page resultPage = ordersService.page(pageBean, queryWrapper);

        //获取集合
        List records = resultPage.getRecords();

        //获取总记录数
        long total = resultPage.getTotal();
        if (records != null) {
            return R.ok().data("records", records).data("total", total);
        } else {
            return R.error().message("没有查到集合数据");
        }

    }


    @ApiOperation(value = "添加订单")
    @PostMapping("/addOders")
    @CrossOrigin
    public R addOrder(@RequestBody Orders orders){
        System.out.println("-----------"+orders);
        orders.setCreattime(new Date());
        boolean save = ordersService.save(orders);
        if (save) {
            return R.ok().data("save",save);
        } else {
            return R.error().message("无法添加俱乐部");
        }

    }
    @ApiOperation(value = "得到登陆用户的预约")
    @GetMapping("/getOrdersByUserId/{uid}")
    public R getOrdersByUserId(@PathVariable("uid") Integer uid){
        Orders o=new Orders();
        o.setPayer(uid);
        QueryWrapper<Orders> q=new QueryWrapper<Orders>(o);
        List<Orders> orders = ordersService.list(q);
        Users user = usersService.getById(uid);
        if (orders!=null) {
            List<Course> course=new ArrayList<Course>();
            for (Orders order : orders) {
                Integer courseid = order.getCourseid();
                Course c = courseService.getById(courseid);
                course.add(c);
            }
            return R.ok().data("orders",orders).data("course",course).data("user",user);
        } else {
            return R.error().message("没有订单");
        }
    }
}

